//
//  LZPublishViewController.m
//  LZHundredJie
//
//  Created by apple on 16/4/25.
//  Copyright © 2016年 m14a.cn. All rights reserved.
//

#import "LZPublishViewController.h"
#import "LZPublishButton.h"
#import "LZPostWordViewController.h"
#import "LZNavigationController.h"

#import "POP.h"

static CGFloat const LZSpringFactor = 10;

@interface LZPublishViewController ()
/** 标语 */
@property (nonatomic, weak) UIImageView *sloganView;
/** 动画时间 */
@property (nonatomic, strong) NSArray *times;
/** 按钮 */
@property (nonatomic, strong) NSMutableArray *buttons;

@end

@implementation LZPublishViewController

#pragma mark - 懒加载

- (NSMutableArray *)buttons
{
    if (_buttons == nil) {
        _buttons = [NSMutableArray array];
    }
    return _buttons;
}

- (NSArray *)times
{
    if (_times == nil) {
        CGFloat interval = 0.0; // 时间间隔
        _times = @[@(5 * interval),
                   @(4 * interval),
                   @(1 * interval),
                   @(0 * interval),
                   @(2 * interval),
                   @(3 * interval),
                   @(6 * interval)];  // 标语的动画时间
                   
    }
    return _times;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 禁止交互
    self.view.userInteractionEnabled = NO;
    // 添加按钮
    [self setupButtons];
    // 添加标语
    [self setupSloganView];
    
}

/**
 1. pop
 1> facebook出品的动画框架
 2> 动画能作用在任何对象上
 3> 能监听到动画的中间值
 
 X == 10  -> X == 100
 
 2. Core Animation
 1> 苹果官方的动画框架
 2> 动画只能作用在CALayer上
 3> 无法监听到动画的中间值
 
 
 // 示例程序 github上去搜
 popping
 LearnCube
 
 https://github.com/sxyx2008/awesome-ios-animation
 
 */

- (void)setupSloganView {
    CGFloat sloganY = ScreenH * 0.2;
    // 标语
    UIImageView *sloganView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
    sloganView.y = sloganY - ScreenH;
    sloganView.centerX = ScreenW * 0.5;
    [self.view addSubview:sloganView];
    self.sloganView = sloganView;
    
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(sloganY);
    anim.springSpeed = 10;
    anim.springBounciness = 10;
     __weak typeof(self) weakSelf = self;
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        // 开始交互
        weakSelf.view.userInteractionEnabled = YES;
    }];
    // CACurrentMediaTime() 获取的是当前时间
    anim.beginTime = CACurrentMediaTime() + [[self.times lastObject] doubleValue];
    [sloganView.layer pop_addAnimation:anim forKey:nil];
}

- (void)setupButtons {
    // 按钮
    // 数据
    NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    NSArray *titles = @[@"发视频", @"发图片", @"发段子",@"发声音", @"审帖", @"离线下载"];
    
    // 一些参数
    NSInteger count = images.count;
    NSInteger maxColsCount = 3; // 一行的列数
    NSInteger rowsCount = (count + maxColsCount - 1) / maxColsCount;
    
    // 按钮尺寸
    CGFloat buttonW = ScreenW / maxColsCount;
    CGFloat buttonH = buttonW * 0.85;
    CGFloat buttonStartY = (ScreenH - rowsCount * buttonH) * 0.5;
    for (NSInteger i = 0; i < count; i++) {
        // 创建、添加
        LZPublishButton *button = [[LZPublishButton alloc] init];
        // 按钮的尺寸为0，如果已经给按钮设置文字了，还是能看见按钮缩成一个点
        // 设置按钮的尺寸为负数，那么就看不见文字了
        button.width = -1;
        // 添加监听事件
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttons addObject:button];
        [self.view addSubview:button];
        // frame
        CGFloat buttonX = (i % maxColsCount) * buttonW;
        CGFloat buttonY = buttonStartY + (i / maxColsCount) * buttonH;
//        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 内容
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY - ScreenH, buttonW, buttonH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
        anim.springSpeed = LZSpringFactor;
        anim.springBounciness = LZSpringFactor;
        // CACurrentMediaTime() 获取的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        [button pop_addAnimation:anim forKey:nil];
    }
}

#pragma mark - 退出操作
- (void)exit:(void (^)())task
{
    // 禁止交互
    self.view.userInteractionEnabled = NO;
    
    // 让按钮执行动画
    for (NSInteger i = 0; i < self.buttons.count; i++) {
        
        LZPublishButton *button = self.buttons[i];
        
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
        anim.fromValue = @(button.layer.position.y);
        anim.toValue = @(button.layer.position.y + ScreenH);
        // CACurrentMediaTime() 获取的是当前时间
        anim.beginTime = CACurrentMediaTime() + [self.times[i] doubleValue];
        [button.layer pop_addAnimation:anim forKey:nil];
        /*
         POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionY];
         self.anim = anim;
         anim.fromValue = @(button.layer.position.y );
         anim.toValue = @(button.layer.position.y - 40);
         anim.springSpeed = LZSpringFactor;
         anim.springBounciness = LZSpringFactor;
         // CACurrentMediaTime() 获取的是当前时间
         anim.beginTime = CACurrentMediaTime() + 0.05;
         [button.layer pop_addAnimation:anim forKey:nil];
         */
    }
    
    // 让标题执行动画
    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerPositionY];
    anim.toValue = @(self.sloganView.layer.position.y + ScreenH);
    anim.beginTime = CACurrentMediaTime() + [[self.times lastObject] doubleValue];
    [self.sloganView.layer pop_addAnimation:anim forKey:nil];
    
    __weak typeof(self) weakSelf = self;
    
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        
        // 可能会做其他事情,block
        if (task)   task();
        
        // 装逼代码
//        !task : ? task();
    }];
}

- (void)buttonClick:(LZPublishButton *)button
{
    /*
//    UISwitch *s = [[UISwitch alloc] init];
//    s.on = YES;
//    [self.view addSubview:s];
    
//    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
//    anim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
//    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
//    anim.duration = 1.0;
    
//    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
//    anim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
//    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
//    anim.springSpeed = 20;
//    anim.springBounciness = 10;
//    [s pop_addAnimation:anim forKey:nil];
    
//    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPosition];
//    anim.fromValue = [NSValue valueWithCGPoint:CGPointZero];
//    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
//    anim.springSpeed = 20;
//    anim.springBounciness = 10;
//    [s.layer pop_addAnimation:anim forKey:nil];
     */
    [self exit:^{
        // 按钮索引
        NSInteger index = [self.buttons indexOfObject:button];
        
        switch (index) {
            case 0:{
                break;
            }
            case 1:{
                break;
            }
                
            case 2: {   // 发段子
                LZPostWordViewController *postWord = [[LZPostWordViewController alloc] init];
                // 或者可以 [UIApplication sharedApplication].keyWindow.rootViewController
                // 在这个viewDidLoad调用中,postWord.navigationController = nil
                // 也就是没有导航条
//                postWord.view.backgroundColor = LZGlobalBg;
                
                [self.view.window.rootViewController presentViewController:[[LZNavigationController alloc] initWithRootViewController:postWord] animated:YES completion:nil];
                break;
            }
            default:
                break;
        }
    }];
    
}
// 点击取消按钮
- (IBAction)cancel {
    [self exit:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self cancel];
}

@end
